package com.example.test.demos.web.entity;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @author Administrator
 */
@Data
public class Fruit {
    private BigDecimal price;
    private int weight;
    private BigDecimal amount;
    private BigDecimal percent;


    /**
     * 设置 price 和 weight，并自动计算 amount
     *
     * @param price  水果单价
     * @param weight 水果重量
     */
    public void setPriceAndWeight(BigDecimal price, int weight) {
        this.price = price;
        this.weight = weight;
        calculateAmount(); // 自动计算 amount
    }

    public void calculateAmount() {
        if (price != null && weight > 0) {
            this.amount = price.multiply(BigDecimal.valueOf(weight));
            if (percent != null){
                this.amount = this.amount.multiply(this.percent);
            }
        } else {
            this.amount = BigDecimal.ZERO;
        }
    }

    /**
     * 设置 price 和 weight和 percent，并自动计算 amount
     *
     * @param price  水果单价
     * @param weight 水果重量
     */
    public void setPercentPriceAndWeight(BigDecimal price, int weight, BigDecimal percent) {
        this.price = price;
        this.weight = weight;
        this.percent = percent;
        calculateAmount(); // 自动计算 amount
    }
}
